package com.hitown.controller.dxhd;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hitown.model.dxhd.DxhdCltsDqc;
import com.hitown.model.dxhd.DxhdCltsTpcl;
import com.hitown.model.dxhd.DxhdJq;
import com.hitown.service.dxhd.DxhdCltsDqcService;
import com.hitown.util.Params;
import com.hitown.util.R;
import com.hitown.util.StringUtils;
import com.hitown.util.UUIDUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
import java.util.Collection;
import java.util.Date;
import java.util.UUID;

/**
 * <p>
 * 车辆态势-盗抢车基本信息 前端控制器
 * </p>
 *
 * @author menu
 * @since 2021-07-19
 */
@RestController
@RequestMapping("/dxhd-clts-dqc")
public class DxhdCltsDqcController {

    @Autowired
    private DxhdCltsDqcService dxhdCltsDqcService;

    @CrossOrigin
    @ApiOperation(value = "分页查询")
    @RequestMapping("selectPage")
    public R selectPage(@ApiIgnore @RequestBody Params<DxhdCltsDqc> params){
        Page<DxhdCltsDqc> page = new Page<>(params.getCurrent(),params.getSize());
        QueryWrapper<DxhdCltsDqc> wrapper =new QueryWrapper<>();
        wrapper.select("BH","DQCZP","DQCZP_DZ","BDQCXX_JDCHPHM","BDQCXX_ZWPPMC","BDQCXX_YWPPMC","BDQCXX_JDCCSYSDM","(select content from t_dictionary t where type='CSYS' and id = BDQCXX_JDCCSYSDM) BDQCXX_JDCCSYSMC",
                "BDQCXX_CLXH","BDQCZXX_XM","BDQCZXX_GMSFHM","CTU","CTM","UTU","UTM");
        //被盗抢车信息-机动车号牌号码
        if(!StringUtils.isEmpty(params.getEntity().getBdqcxxJdchphm())) {
            wrapper.like("BDQCXX_JDCHPHM", params.getEntity().getBdqcxxJdchphm());
        }
        //被盗抢车信息-车辆型号
        if(!StringUtils.isEmpty(params.getEntity().getBdqcxxClxh())) {
            wrapper.eq("BDQCXX_CLXH", params.getEntity().getBdqcxxClxh());
        }
        IPage<DxhdCltsDqc> pageVo = dxhdCltsDqcService.page(page,wrapper);
        for (DxhdCltsDqc e: pageVo.getRecords()) {
            if(e.getDqczp()!=null){
                String base64Str = "data:image/jpg;base64,"+ DatatypeConverter.printBase64Binary(e.getDqczp());
                e.setDqczpDz(base64Str);
            }
        }
        return R.ok().put("data",pageVo);

    }


    @ApiOperation(value = "根据id查询记录")
    @RequestMapping("getById")
    public R getById(@RequestParam String Id){
        DxhdCltsDqc result = dxhdCltsDqcService.getById(Id);
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "保存单条记录")
    @PostMapping("save")
    public R save(@RequestBody DxhdCltsDqc entit){
        entit.setBh(UUIDUtils.getUUID());
        Boolean result = dxhdCltsDqcService.save(entit);
        return R.ok().put("data",result);
    }


    @ApiOperation(value = "批量保存")
    @RequestMapping("saveBatch")
    public R saveBatch(@RequestBody Collection<DxhdCltsDqc> entityList){
        Boolean result = dxhdCltsDqcService.saveBatch(entityList);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("updateById")
    public R updateById(@RequestBody DxhdCltsDqc entit){
        entit.setUtm(new Date());
        entit.setDqczp(null);
        entit.setDqczpDz(null);
        Boolean result = dxhdCltsDqcService.updateById(entit);
        return R.ok().put("data",result);

    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("update")
    public R update(@RequestParam String bh,@RequestParam("file") MultipartFile file) throws IOException {
        DxhdCltsDqc entity = new DxhdCltsDqc();
        entity.setDqczp(file.getBytes());
        if(!StringUtils.isEmpty(bh) && !bh.equals("undefined")){
            entity.setBh(bh);
            dxhdCltsDqcService.updateById(entity);
        }else{
            entity.setBh(UUID.randomUUID().toString());
            dxhdCltsDqcService.save(entity);
        }
        return R.ok().put("data",entity.getBh());
    }

    @ApiOperation(value = "根据 ID 选择修改")
    @RequestMapping("handleRemove")
    public R handleRemove(@RequestBody DxhdCltsDqc entity) throws IOException {
        entity.setDqczp(new byte[0]);
        dxhdCltsDqcService.updateById(entity);
        return R.ok().put("data",entity.getBh());
    }

    @ApiOperation(value = "根据ID 批量更新")
    @RequestMapping("updateBatchById")
    public R updateBatchById(@RequestBody Collection<DxhdCltsDqc> entityList){
        Boolean result = dxhdCltsDqcService.updateBatchById(entityList);
        return R.ok().put("data",result);

    }

    @CrossOrigin
    @ApiOperation(value = "根据 ID 删除")
    @RequestMapping("removeById")
    public R removeById(@RequestParam String Id){
        Boolean result = dxhdCltsDqcService.removeById(Id);
        return R.ok().put("data",result);

    }


    @ApiOperation(value = "删除（根据ID 批量删除）")
    @RequestMapping("removeByIds")
    public R removeByIds(@RequestBody Collection<String> Ids){
        Boolean result = dxhdCltsDqcService.removeByIds(Ids);
        return R.ok().put("data",result);
    }
}

